home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 303 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.4 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: BAFlorma@softart.com (BAFlorma)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Give operator. a chance
  5. Date: 06 Feb 1996 10:30:17 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <116659a0@softart.com>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: Mon, 5 Feb 1996 15:18:14 -0500
  11. Content-Description: cc:Mail note part
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMRef/Ey4NqrwXLNJAQE9OAIAvfugvQukcA886NOGmimoKStYfmMAOy/p
  14.     2VvL2Mp3CVUMXSRlZ4In2pduuiUbUHPen22k1p2BmDJTK02dKCwYxQ==
  15.     =Pxtu
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. I'm not sure that this is appropriate for comp.std.c++, but I suppose that's 
  19. what moderation is for.  Maybe this is an issue for the subsequent standard 
  20. (C++ 2007?).
  21.  
  22. [Moderator's note: we do accept articles that deal with C++ language
  23. design issues, even if they discuss a possible change to the language
  24. that's unlikely to make it into the standard.  Consider, for example,
  25. the recent operator. discussion.  Note also that questions about why
  26. the language was designed one way, instead of another, are usually
  27. legitimate topics for this group.  mha]
  28.  
  29. I've never been particularly happy with the -> operator in C, much less C++.  
  30. It strikes me as being quite redundant.  In C, an expression with the form 
  31. p.m has no legitimate meaning if p is an instance of a pointer type.  To 
  32. access the m member of the object addressed by p one needs to write p->m.  
  33. In C this was just a little ugliness, but in C++, where one may legitimately 
  34. wish to alter the access mechanism for an object from ponters to references 
  35. or vice versa over the lifetime of a project, it's a downright nuisance.  If 
  36. the compiler knows that p is a pointer type and therefore has no m member 
  37. itself, why can't it automatically dereference the pointer in the same manner 
  38. that the built in operator() automatically dereferences a pointer to function?
  39.  
  40. Obviously this would significantly blur the distinction between pointers and  
  41. references.  Perhaps this would have been a good thing.  Do we really need  
  42. pointers and references in the same language?  Wouldn't the following rules,  
  43. had they been added to C++ around 1983, have been sufficient to implement the  
  44. basic functionality of references without adding the new concept?
  45.  
  46. 1. Whenever the expression p.m appears and p has no member named m, the 
  47.    expression is replaced by (*p).m if and only if there is an operator* 
  48.    applicable to the type of p.
  49.  
  50. 2. Whenever the expression f(x) appears and there exists no overload of 
  51.    f which takes an argument matching the type of x, the expression is 
  52.    replaced by either f(*x) or f(&x) depending on which yields a valid 
  53.    expression.  If both do, the expression is ambiguous.
  54.  
  55. Rule 2 would extend to overloaded operators as well, so that the b - c case 
  56. that Dr. Stroustrup mentions in D&E 3.7 would implicitly be replaced by 
  57. operator-(&b, &c) when b and c are large objects.
  58.  
  59. With these rules in place, could not delegation be implemented transparently 
  60. via a "smart pointer" class and without any need to overload operator. (or 
  61. operator-> for that matter)?
  62.  
  63. Perhaps I've missed some crucial aspect of references.  I haven't devoted a 
  64. great deal of contemplation to this, but the only feature of references not 
  65. replicated by the above that I can see right offhand is that pointers lack 
  66. the prohibition against rebinding.  Obviously there would be occations when 
  67. coding in a language implemented with the above rules, especially when 
  68. applying operators, that pointers would not be as transparent as the current 
  69. C++ references are.  Nevertheless, I wonder if this might not have been a 
  70. cleaner approach overall.
  71.  
  72. I suppose this would have been a much better topic for discussion a decade 
  73. ago, but I was still a lisp hacker back then and had barely heard of C++.  
  74. :-)
  75.  
  76. --Bruce
  77.  
  78. ----------------------------------------------------------------------------- 
  79. Bruce A. Florman    \ Software Engineer specializing in OO Languages and C++. 
  80. bflorman@indy.net    \ Former Lisp hacker.  Former skydiver.  Ex-Virginian. 
  81. BAFlorman@softart.com \ Fan of Joe Gibbs Racing and the Washington Redskins. 
  82. -----------------------------------------------------------------------------
  83.  "Tragically, this serene metropolis lies directly beneath the Hoover Dam."
  84. ---
  85. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  86.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy is
  87.   in http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  88.